package com.example.demo.controller;


import com.example.demo.service.UserService;
import com.example.demo.vo.RespInfo;
import com.example.demo.vo.user.LoginParam;
import com.example.demo.vo.user.UserParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 用户信息表(User)表控制层
 *
 * @author yuan
 * @since 2023-01-16 13:57:23
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/query/{id}")
    public RespInfo queryById(@PathVariable("id") Long id) {
        return userService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param param 实体
     * @return 新增结果
     */
    @RequestMapping(value = "/add", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public RespInfo add(@RequestBody UserParam param,HttpServletRequest request) {
        return userService.insert(param,request);
    }

    /**
     * 编辑数据
     *
     * @param param 实体
     * @return 编辑结果
     */
    @RequestMapping(value = "/edit", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public RespInfo edit(@RequestBody UserParam param) {
        return userService.update(param);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/del/{id}")
    public RespInfo deleteById(@PathVariable("id") Long id) {
        return userService.deleteById(id);
    }


    /**
     * 登录接口
     *
     * @param param 实体
     * @return 新增结果
     */
    @RequestMapping(value = "/login", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public RespInfo Login(@RequestBody LoginParam param) {
        return userService.login(param);
    }


    /**
     * 注销接口
     *
     * @param
     * @return 新增结果
     */
    @RequestMapping(value = "/logout", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public RespInfo logout(HttpServletRequest request) {
        return userService.logout(request);
    }
}

